package cn.qdhlm.access;

import com.healthmarketscience.jackcess.*;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class AccessDbHelper {
    /**
     * 打开数据库文件
     *
     * @param dbPath 数据库文件路径
     * @return database
     */
    public static Database open(String dbPath) {
        if (dbPath.endsWith(".mdb") || dbPath.endsWith(".accdb")) {
            try {
                return DatabaseBuilder.open(new File(dbPath));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 读取 数据库 所有表
     *
     * @param database 数据库
     * @return allTableDataMap
     */
    public static Map<String, List<Map<String, Object>>> read(Database database) {
        try {
            Set<String> tableNames = database.getTableNames();
            Map<String, List<Map<String, Object>>> allTableDataMap = new HashMap<>(tableNames.size());
            if (tableNames.size() > 0) {
                for (String tableName : tableNames) {
                    allTableDataMap.put(tableName, read(database, tableName));
                }
            }
            return allTableDataMap;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取 数据库指定表
     *
     * @param database  数据库
     * @param tableName 表名
     * @return tableDataMapList
     */
    public static List<Map<String, Object>> read(Database database, String tableName) {
        Table table = null;
        try {
            table = database.getTable(tableName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (null != table) {
            List<Map<String, Object>> tableDataMapList = new ArrayList<>();

            List<? extends Column> columns = table.getColumns();
            if (columns.size() > 0) {
//                for (Column column : columns) {
//                    try {
//                        System.out.println("name: " + column.getName());
//                        System.out.println("type: " + column.getType());
//                        System.out.println("precision: " + column.getPrecision());
//                        System.out.println("scale: " + column.getScale());
//
//                        PropertyMap properties = column.getProperties();
//                        System.out.println(properties);
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }

                Map<String, Object> dataMap;
                for (Row row : table) {
                    dataMap = new HashMap<>(columns.size());
                    for (Column column : columns) {
                        dataMap.put(column.getName(), row.get(column.getName()));
                    }
                    tableDataMapList.add(dataMap);
                }
            }
            return tableDataMapList;
        }
        return null;
    }
}
